package com.sc.test;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.sc.test.bean.News;
import com.sc.test.bean.RequestData;
import com.sc.test.bean.URLS;
import com.sc.test.utils.Util;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Shen Chao on 2017-12-24-024.
 */
public class OkhttpActivity extends AppCompatActivity {
    OkHttpClient client = new OkHttpClient();
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okhttp);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_ok_get_sync, R.id.btn_ok_get_asyn, R.id.btn_okhttp_post, R.id.btn_okhttp_post_gson
            , R.id.btn_okhttp_post_fromjson})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_ok_get_sync:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Request request = new Request.Builder()
                                .url(URLS.BAIDU_URL)
                                .build();
                        Call call = client.newCall(request);
                        try {
                            Response response = call.execute();
                            String result = response.body().string();
                            if (response.isSuccessful()) {
                                Util.openNewActivity(OkhttpActivity.this, ResultActivity.class, result);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                System.out.println(Thread.currentThread().getId());
                break;
            case R.id.btn_ok_get_asyn:
                Request request = new Request.Builder()
                        .url(URLS.BAIDU_URL)
                        .build();
                Call call = client.newCall(request);
                try {
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String result = response.body().string();
                            Util.openNewActivity(OkhttpActivity.this, ResultActivity.class, result);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_okhttp_post:
                FormBody formbody = new FormBody.Builder()
                        .add("type", "type")
                        .add("key", URLS.JUHE_APPKEY)
                        .build();
                Request request1 = new Request.Builder()
                        .url(URLS.NEWS_URL)
                        .post(formbody)
                        .build();
                Call call1 = client.newCall(request1);
                call1.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Util.openNewActivity(OkhttpActivity.this, ResultActivity.class, result);
                    }
                });
                break;
            case R.id.btn_okhttp_post_gson:
                FormBody formbody2 = new FormBody.Builder()
                        .add("type", "type")
                        .add("key", URLS.JUHE_APPKEY)
                        .build();
                Request request2 = new Request.Builder()
                        .url(URLS.NEWS_URL)
                        .post(formbody2)
                        .build();
                Call call2 = client.newCall(request2);
                call2.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Gson gson = new Gson();
                        String result = response.body().string();
                        News news = gson.fromJson(result, News.class);
                        Util.openNewActivity(OkhttpActivity.this, ResultActivity.class, news.toString());
                    }
                });
                break;
            case R.id.btn_okhttp_post_fromjson:
                RequestData requestData = new RequestData();
                requestData.setType("top");
                requestData.setKey(URLS.JUHE_APPKEY);
                Gson gson = new Gson();
                String json = gson.toJson(requestData);
                System.out.println(json);
                RequestBody requestBody = RequestBody.create(JSON, json);
                Request request3 = new Request.Builder()
                        .url(URLS.NEWS_URL)
                        .post(requestBody)
                        .build();
                Call call3 = client.newCall(request3);
                call3.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Util.openNewActivity(OkhttpActivity.this, ResultActivity.class, result);
                    }
                });
                break;
        }
    }
}
